package com.chance.web.admin.schedule;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.chance.common.constant.RedisConstant;
import com.chance.common.minio.MinioProperties;
import com.chance.common.utils.RedisImagesAddUtil;
import com.chance.model.entity.GraphInfo;
import com.chance.model.entity.LeaseAgreement;
import com.chance.model.enums.LeaseStatus;
import com.chance.web.admin.service.GraphInfoService;
import com.chance.web.admin.service.LeaseAgreementService;
import io.minio.MinioClient;
import io.minio.RemoveObjectArgs;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Component
public class ScheduledTasks {

    @Autowired
    private LeaseAgreementService leaseAgreementService;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    MinioClient minioClient;

    @Autowired
    MinioProperties minioProperties;

    @Autowired
    RedisTemplate redisTemplate;

    @Scheduled(cron = "0 0 0 * * *")
    public void checkLeaseStatus() {
        LambdaUpdateWrapper<LeaseAgreement> leaseAgreementLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        Date now = new Date();
        leaseAgreementLambdaUpdateWrapper.le(LeaseAgreement::getLeaseEndDate, now);
        leaseAgreementLambdaUpdateWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING);
        leaseAgreementLambdaUpdateWrapper.set(LeaseAgreement::getStatus, LeaseStatus.EXPIRED);
        leaseAgreementService.update(leaseAgreementLambdaUpdateWrapper);
    }

    @Scheduled(cron = "0/30 * * * * *")
    public void  checkremoveImage() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        List<String> imgUrls = graphInfoService.getRemoveUrl();

        // 删除数据库图片链接的图片
        if (imgUrls != null && !imgUrls.isEmpty()) {
                for (String imgUrl : imgUrls) {
                    long count = graphInfoService.count(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getUrl, imgUrl));
                    if (count <= 0) {
                        String fileName = RedisImagesAddUtil.parseUrl(imgUrl);
                        minioClient.removeObject(RemoveObjectArgs.builder().bucket(minioProperties.getBucketName()).object(fileName).build());
                    }
                }
            graphInfoService.removeUrl(imgUrls);
        }

        // 删除数据库未保存图片链接的图片
        Set difference = redisTemplate.opsForSet().difference(RedisConstant.ALL_IMAGES_REDIS, RedisConstant.DB_IMAGES_REDIS);
        if (difference != null && !difference.isEmpty()) {
            for (Object fileName : difference) {
                minioClient.removeObject(RemoveObjectArgs.builder().bucket(minioProperties.getBucketName()).object(fileName.toString()).build());
            }
        }
    }
}
